<template>
  <div style="padding: 8px">
    <el-form
      :model="formData"
      :rules="rules"
      size="mini"
      labelPosition="right"
      labelWidth="80px"
      ref="form"
    >
      <el-row :gutter="0">
        <div class="l-rblock">
          <div class="border pd-16">
            <div class="ptitle">
              {{ $t("供应商评估报告") }}
            </div>
            <div class="mt-16">
              <div v-if="notAssess" class="form_flex_center">
                <div class="font_size_14 font_color">
                  {{ $t("请先完成供应商评估才可进行转正申请！") }}
                </div>
              </div>
              <div v-else>
                <div class="learun-form-title">{{ $t("基本信息") }}</div>
                <div class="form_block flex flex-center flex-c-between">
                  <div>
                    {{ $t("供应商编号：" + supplierInfo.f_Number) }}
                  </div>
                  <div>
                    {{ $t("供应商名称：" + supplierInfo.f_Name) }}
                  </div>
                  <div>
                    {{ $t("供应商负责人：" + supplierInfo.f_Person) }}
                  </div>
                  <div>
                    {{ $t("供应商类型：" + supplierInfo.f_Type) }}
                  </div>
                  <div>
                    {{ $t("添加人：" + supplierInfo.f_CreateUserName) }}
                  </div>
                  <div>
                    {{ $t("创建日期：" + supplierInfo.f_CreateDate) }}
                  </div>
                  <div>
                    {{ $t("经营范围：" + supplierInfo.f_Scope) }}
                  </div>
                  <div>
                    {{ $t("手机号：" + supplierInfo.f_Phone) }}
                  </div>
                </div>

                <!-- 评估查看 -->
                <assess-form
                  v-if="supplier_form"
                  :isReadOnly="true"
                  :id="id"
                  type="0"
                />

                <div class="flex flex-c-center mt-16">
                  <el-button
                    plain
                    @click="showLookForm"
                    :icon="
                      supplier_form ? 'el-icon-arrow-up' : 'el-icon-arrow-down'
                    "
                    >{{
                      $t(!supplier_form ? "展开报告" : "收起报告")
                    }}</el-button
                  >
                </div>
              </div>
            </div>
          </div>

          <template v-if="lr_formLookAuth('f_Description')">
            <div class="learun-form-title mt-16">
              {{ $t("转正说明") }}
            </div>
            <div class="form_block">
              <el-form-item :label="$t('转正说明')" prop="f_Description">
                <el-input
                  type="textarea"
                  style="width: 500px"
                  :disabled="
                    !lr_formEditAuth(
                      'f_Description',
                      false || notAssess || isReadOnly
                    )
                  "
                  v-model="formData.f_Description"
                  :placeholder="$t('请输入')"
                />
              </el-form-item>
            </div>
          </template>

          <template v-if="lr_formLookAuth('f_File')">
            <div class="learun-form-title mt-16">
              {{ $t("附件上传") }}
            </div>
            <div class="form_block">
              <l-upload
                drag
                maxSize="10"
                sizeType="MB"
                :limit="3"
                showFileList="uploadTheme1"
                :disabled="
                  !lr_formEditAuth('f_File', false || notAssess || isReadOnly)
                "
                v-model="formData.f_File"
              />
            </div>
          </template>
        </div>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import AssessForm from "./assessForm";
const api = window.$api.erpcase.supplier;
export default {
  props: {
    isReadOnly: {
      type: Boolean,
      default: false,
    },
  },
  components: {
    AssessForm,
  },
  data() {
    return {
      moduleCode: "erpCase_supplierformal",
      supplierInfo: {
        f_Number: "",
        f_Name: "",
        f_Person: "",
        f_Phone: "",
        f_Scope: "",
        f_CreateDate: "",
        f_CreateUserName: "",
        f_Type: "",
      },

      formData: {
        f_Description: "", // 转正备注
        f_File: "", // 附件
      },
      rules: {
        f_Description: [{ required: true, message: "请输入" }],
      },
      supplier_form: false,
      notAssess: false,
      id: "",
    };
  },
  computed: {},
  methods: {
    async init(wfParams) {
      if (wfParams) {
        const { id, notAssess } = wfParams;
        if (id) {
          this.id = id;
        }
        if (notAssess) {
          this.notAssess = notAssess;
        } else {
          await this.loadSupplierInfo(this.id);
        }
      }
      return false;
    },
    // 加载供应商信息
    async loadSupplierInfo(id) {
      const supplierInfo = await this.$awaitWraper(api.get(id));
      if (supplierInfo) {
        supplierInfo.f_CreateDate = this.$formatDate(
          supplierInfo.f_CreateDate,
          "yyyy-MM-dd"
        );
        this.supplierInfo = supplierInfo;
      }
    },

    // 重置表单
    resetForm() {
      this.$formClear(this.$refs.form);
    },
    // 校验表单
    async validateForm() {
      if (!(await this.$formValidateWraper(this.$refs.form))) {
        return false;
      }
      return true;
    },

    // 设置表单数据
    setForm(data) {
      this.formData = data;
    },
    // 获取表单数据
    getForm() {
      const formData = this.$deepClone(this.formData);
      if (this.id) {
        formData.f_SupplierId = this.id;
      }
      return formData;
    },

    async loadFormData({ keyValue /*,params,node*/ }) {
      //  keyValue 流程中相当于流程processId,params 传递参数,node 流程节点
      const supplierformal = await this.$awaitWraper(api.getFormal(keyValue));
      if (supplierformal) {
        this.id = supplierformal.f_SupplierId;
        await this.loadSupplierInfo(supplierformal.f_SupplierId);
        this.setForm(supplierformal);
        return true;
      } else {
        return false;
      }
    },

    async saveForm({ keyValue, isEdit /*,code,node*/ }) {
      // isEdit 是否更新数据, keyValue 流程中相当于流程processId,code 表示流程中的操作码,node 流程节点
      const postData = this.getForm();
      console.log(isEdit);
      if (isEdit) {
        // 编辑
        return await this.$awaitWraper(api.updateFormal(keyValue, postData));
      } else {
        postData.f_Id = keyValue;
        // 新增
        return await this.$awaitWraper(api.addFormal(postData));
      }
    },

    //  展开
    async showLookForm() {
      this.supplier_form = !this.supplier_form;
    },
  },
};
</script>